﻿namespace Collections;

public class SortedListItem
{
    public void MySortedList()
    {
        // create a sortedlist
        SortedList<int, string> employees = new SortedList<int, string>()
        {
            {
                101, "Jack"
            },
            {
                102, "Tom"
            },

            {
                103, "Jerry"
            },
            {
                104, "Back"
            },
        };


        // Add element
        employees.Add(105, "Anna");

        // Remove element
        employees.Remove(103);

        // foreach
        foreach (KeyValuePair<int, string> item in employees)
        {
            Console.WriteLine(item.Key + " " + item.Value);
        }

        // get value based on key
        string name = employees[101];

        // search for specific key
        bool k = employees.ContainsKey(101);
        Console.WriteLine("\n 101 exists:" + k);

        // search for specific value
        bool v = employees.ContainsValue("Anna");
        Console.WriteLine("\n Anna exists:" + v);

        // index of specific key
        int ki = employees.IndexOfKey(101);

        // index of specific value
        int vi = employees.IndexOfValue("Anna");

        // keys
        foreach (int item in employees.Keys)
        {
            Console.WriteLine(item);
        }
        
        // values
        foreach (string item in employees.Values)
        {
            Console.WriteLine(item);
        }
    }
}